package com._58city.spark.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.spark.SparkConf;
import org.apache.spark.broadcast.Broadcast;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.kafka.KafkaUtils;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com._58city.spark.app.ext.DaoUtil;
import com._58city.spark.app.ext.dto.BelongCate;
import com._58city.spark.app.ext.dto.DispCity;
import com._58city.spark.app.ext.dto.ResumeSource;
import com._58city.spark.app.mr.MrKafkaResumeAdd;

/**
 * 新增简历实时统计
 */
public class KafkaResumeAddStreaming {

	private static final int batchInterval = 2000; // 切片固定2s
	/**
	 * 运行参数3个：  kafka_topic 消费者ID(如ecdata_group) 接收线程数
	 * @param args
	 */
	public static void main(String[] args) {
		String kafka_topic = args[0]; //Kafka的topic，从运行参数传递进来
		String groupID = args[1];
		int numStreams = Integer.parseInt(args[2]); //开启几个Input DStream接收端

		AbstractApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		DaoUtil.init(context); // 初始化DAO，主要同步几个字典表
		CacheUtil.init(); // 初始化Redis，主要用于统计后数据的存储

		SparkConf conf = new SparkConf()
				.set("spark.streaming.unpersist", "true")
				// Spark来计算哪些RDD需要持久化，这样有利于提高GC的表现。
				.set("spark.default.parallelism", "8")
				// reduceByKeyAndWindow执行时启动的线程数，默认是8个
				.set("spark.yarn.driver.memoryOverhead", "1024")
				.set("spark.yarn.executor.memoryOverhead", "1024")
				.set("spark.storage.memoryFraction", "0.5")
				.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
				.set("spark.kryo.registrator", "com._58city.spark.app.kryo.Registrator");

		JavaStreamingContext jssc = new JavaStreamingContext(conf, new Duration(batchInterval));

		Map<Long, BelongCate> belongCateMap = CacheUtil.belongCateMap();
		Map<Long, DispCity> city_map = CacheUtil.cityMap();
		Map<Integer, ResumeSource> resume_map = CacheUtil.resumeSourceMap();

		Broadcast<Map<Long, BelongCate>> bcBelongCateMap = jssc.sparkContext().broadcast(belongCateMap);
		Broadcast<Map<Long, DispCity>> bc_city_map = jssc.sparkContext().broadcast(city_map);
		Broadcast<Map<Integer, ResumeSource>> bc_resume_map = jssc.sparkContext().broadcast(resume_map);

		// 从kafka获取数据
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put(kafka_topic, 1);
		
		Map<String, String> kafkaParams = new HashMap<String, String>();
		kafkaParams.put("group.id", groupID);
		kafkaParams.put("auto.offset.reset", "largest");
		kafkaParams.put("fetch.message.max.bytes", String.valueOf(20*1024*1024));
		kafkaParams.put("zookeeper.connect" ,"10.126.99.105:2181,10.126.99.196:2181,10.126.81.208:2181,10.126.100.144:2181,10.126.81.215:2181/58_kafka_cluster");
		
		List<JavaPairDStream<String, String>> kafkaStreams = new ArrayList<JavaPairDStream<String, String>>(numStreams);
		
		for (int i = 0; i < numStreams; i++) {
			kafkaStreams.add(KafkaUtils.createStream(jssc,String.class,String.class,kafka.serializer.StringDecoder.class,kafka.serializer.StringDecoder.class, kafkaParams, map, StorageLevel.MEMORY_AND_DISK_SER()));
		}

		MrKafkaResumeAdd mr = new MrKafkaResumeAdd(
				new String[] { "${targetCateID}", "${targetAreaID}", "${platform}" }, batchInterval);

		mr.setBcBelongCateMap(bcBelongCateMap);
		mr.setBc_city_map(bc_city_map);
		mr.setBc_resume_map(bc_resume_map);

		List<JavaPairDStream<String, Integer>> mapStreams = mr.mapPair(kafkaStreams);

		// 将Stream全部聚合到第一个上
		JavaPairDStream<String, Integer> unionStream = null;
		if (mapStreams.size() > 1) {
			unionStream = jssc.union(mapStreams.get(0), mapStreams.subList(1, mapStreams.size()));
		} else {
			unionStream = mapStreams.get(0);
		}

		JavaPairDStream<String, Integer> reduceStream = mr.reducePair(unionStream);

		// 输出结果处理
		mr.foreachRDD(reduceStream);

		jssc.start();
		jssc.awaitTermination();
	}
}
